#######################################################################################
### Script file to reproduce the figures in Chapter 3
###
### Created: 2-23-23
### Modified: 
###
#######################################################################################

library(foreign)
library(ggplot2)
library(lattice)
library(fields)
library(scales)
library(gridExtra)


################################################################################
### Figure 5: Sophia perceptions for British parties from 1983-2019
################################################################################

## UK results
uk.data <- read_dta('uk.dta')

uk.plot <- ggplot(data = uk.data, aes(x = year, y = sophiaMean, linetype = as.factor(cmp))) + geom_line()
uk.plot <- uk.plot + ylab("Perceptions (Sophia)") + xlab("") 
uk.plot <- uk.plot + annotate(geom="text", x=1984, y=2.75, label="Labour")
uk.plot <- uk.plot + annotate(geom="text", x=1986, y=4.3, label="Liberal Democrats")
uk.plot <- uk.plot + annotate(geom="text", x=2016, y=5.9, label="UKIP")
uk.plot <- uk.plot + annotate(geom="text", x=1985, y=7.5, label="Conservatives")
uk.plot <- uk.plot + theme_classic() + theme(legend.position = "none")
uk.plot


#######################################################################################
### Figure 6: Illustration of the persistence and effects of positions across dynamic model specifications
#######################################################################################

### Load the data
adl <- read.delim("Estimates.txt", sep = "\t", header = TRUE)

adl$mname <- factor(adl$mname, levels = c("General ADL", "Partial Adjustment", "Dead Start", "Finite Distributed Lag", "Static"))
adl_2 <- adl[rowSums(is.na(adl)) > 0,]
adl_2$effect <- 0

### Sort the model names so they appear properly in the facet_wrap
adl <- within(adl, mname <- factor(mname, levels = c("General ADL", "Partial Adjustment", "Dead Start", "Finite Distributed Lag", "Static")))
adl_2 <- within(adl_2, mname <- factor(mname, levels = c("General ADL", "Partial Adjustment", "Dead Start", "Finite Distributed Lag", "Static")))

### Sorted by appearance in model and tables
xlimits <- c("1","2","3","4")
xlabels <- c("LTE of Manifesto Positions", expression(paste("STE of Manifesto Positions"[t-1])), expression(paste("STE of Manifesto Positions"[t])), "Persistence")

pd <- position_dodge(0.5)
linesize <- 0.75
pointsize <- 1.5

rc.d <- ggplot(data = adl)
rc.d <- rc.d + geom_point(aes(x = xaxis, y = effect), position = pd, size = pointsize)
rc.d <- rc.d + geom_point(data = adl_2, aes(x = xaxis, y = effect), shape = 1, position = pd, size = pointsize)
rc.d <- rc.d + geom_linerange(aes(x = xaxis, ymin = effect_lo, ymax = effect_hi), position = pd, size = linesize)
rc.d <- rc.d + facet_wrap(~ mname, ncol = 3)
rc.d <- rc.d + geom_hline(aes(yintercept = 0), linetype = "dashed", size = 0.25)
rc.d <- rc.d + xlab("") + ylab("Estimate") 
rc.d <- rc.d + scale_x_discrete(limits = xlimits, labels = xlabels) + scale_color_manual(values = c('#000000', '#FF0033', '#0066CC'))
rc.d <- rc.d + coord_flip() + theme_classic() + theme(legend.title=element_blank(), legend.position="bottom")
rc.d



#######################################################################################
### Figure 7: Dynamic simulation of the changes in perceptions in response to a one-unit increase in positions across dynamic model specifications
#######################################################################################

### Load the data
ds <- read.delim("DynSim.txt", sep = "\t", header = TRUE)

### Sort the model names so they appear properly in the facet_wrap
ds <- within(ds, model <- factor(model, levels = c("General ADL", "Partial Adjustment", "Dead Start", "Finite Distributed Lag", "Static")))

# Effects across TS Models: t through t+3
ds.f1 <- ggplot(data=subset(ds, t <= 3), aes(group=model)) + geom_linerange(aes(x=t, ymin = lower_90, ymax = upper_90))
ds.f1 <- ds.f1 + geom_point(aes(x=t, y=effect))
ds.f1 <- ds.f1 + facet_grid(cols = vars(model))
ds.f1 <- ds.f1 + xlab("") + ylab("Change in Voters' Perceptions")
ds.f1 <- ds.f1 + scale_y_continuous(breaks = seq(0,1.25,by=0.25)) 
ds.f1 <- ds.f1 + scale_x_continuous(breaks = seq(0,3,by=1), labels=c("t", "t+1", "t+2", "t+3"))
ds.f1 <- ds.f1 + theme_bw() + theme(legend.position="none")
ds.f1

